package com.kingbull.web.mapper.extend;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

//main mapper
public interface IDCardOrderDao {

    @Select("select o.id,o.total_amount as billNum,CONCAT(s.brand_name,\"●\",s.material_name) as cementTypes,c.INSTITUTION_NAME as supplier\n" +
            "from t_sales_order o\n" +
            "join v_material_brand s on s.id = o.material_id\n" +
            "join t_customer_list c on c.ID = o.customer_id and c.customer = 1\n" +
            "where o.orderType = IFNULL((\n" +
            "select case when xs>0 then 1 when ycl>0 then 2 when dl>0 then 3 when qt>0 then 4 else -1 end from(select\n" +
            "\t\tIFNULL(SUM(case when orderType=1 then 1 else 0 end ),0) xs,\n" +
            "\t\tIFNULL(SUM(case when orderType=2 then 1 else 0 end ),0) ycl,\n" +
            "\t\tIFNULL(SUM(case when orderType=3 then 1 else 0 end ),0) dl,\n" +
            "\t\tIFNULL(SUM(case when orderType=4 then 1 else 0 end ),0) qt\n" +
            "\t\tfrom t_sales_order\n" +
            "\t\twhere idCardNo = #{code}\n" +
            "\t\tand 'status' = 0\n" +
            "\t\tand quite_flag = 0\n" +
            "\t\tand company_code = #{company}\n" +
            "    and (TIMESTAMPDIFF(MINUTE,create_date,SYSDATE())/60 < 24 or incoming_state = 1)\n" +
            "\t)x\n" +
            "),-1) and o.idCardNo = #{code} and o.status = 0 and o.quite_flag = 0 and o.company_code = #{company}\n" +
            "and (TIMESTAMPDIFF(MINUTE,o.create_date,SYSDATE())/60 < 24 or o.incoming_state = 1)")
    List<Map<String,Object>> getOrder(@Param("code") String code, @Param("company")  String company);
}
